#include "nw_client_pal.h"
#include "stdio.h"
#include "charger.h"
#include "string.h"
#include "rtc.h"
#include "record.h"
#include "utility.h"

static const struct ClientCallback g_client_callback={
    nw_send_touch_charge_idle,
    nw_send_touch_charge_stoped,
    nw_send_touch_error_occured,
    nw_send_touch_error_recovered,
    nw_send_touch_card_start_charge,
    nw_charger_state_reup,
};

uint8 isexist_alarm(struct t_pal_alarm *alarm,uint8 *alarmcode)
{
	
	uint32 len = alarm->cnt;
  uint32 i = 0;
	for(;i<len;i++)
	{
		if(0 == strcmp((const char *)alarmcode,(const char *)(alarm->alarminfo+i)))
		{
		   return 1;
		}		
	}
	return 0;
}
uint8 ishave_alarm(struct t_pal_alarm *alarm)
{
	 if(alarm->cnt)
	 {
		  return 1;
	 }
	 return 0;
}
void add_alarm(struct t_pal_alarm *alarm,uint8 *alarmcode)
{
	if(isexist_alarm(alarm,alarmcode))
	{
		printf("is exist\n");
		return;
	}
	uint32 len = alarm->cnt;
  sprintf((char *)(alarm->alarminfo+len),"%s",alarmcode);
	alarm->cnt += 1;
}

void detele_alarm(struct t_pal_alarm *alarm,uint8 *alarmcode)
{
	uint32 len = alarm->cnt;
	uint32 i = 0;
	uint32 pos = 0;
	
	if(0 == len)
	{
		 return ;
	}
	if(!isexist_alarm(alarm,alarmcode))
	{
		printf("is not exist\n");
		return;
	}
	for(;i<len;i++)
	{
		if(0 == strcmp((const char *)alarmcode,(const char *)(alarm->alarminfo+i)))
		{
		   pos = i;
		   break;
		}	
	}
	while(pos+1<len)
	{  
	  sprintf((char *)(alarm->alarminfo+pos),"%s",(char *)(alarm->alarminfo+pos+1));
	  pos +=1 ;
	}
	alarm->cnt -= 1;
}

void print_alarm(struct t_pal_alarm *alarm)
{
 
	uint32 len = alarm->cnt;
	uint32 i = 0;
	
  printf("print_alarm len:%d\r\n",len);
	
	for(;i<len;i++)
	{
		printf("%s\n",(char *)(alarm->alarminfo+i));
	}
}

void pal_set_callback(void){
    dev_set_client_callback(&g_client_callback);
}
void pal_init(void){
    pal_set_callback();
    record_init();
}

struct ClientUserGunInfo *pal_get_user_client_gun_data(){
    return dev_get_user_client_gun_data();
}
struct  ClientUserBmsInfo *pal_get_user_client_bms_data(){
    return dev_get_user_client_bms_data();
}

uint8 pal_getserverip(uint8 *ip)
{
  sprintf((char *)ip,"%s","193.112.182.48");
	
	return 1;
}
uint32 pal_getserverport(void)
{
	return 2417 ;
}

uint8 pal_getconnect_sta(struct ClientUserGunInfo * userinfo)
{
	if(!userinfo->car_connection_stat)//为0时代表枪未连接
	{
		return 0;
	}
	return 1;
}
uint8 pal_getac_input_v_overalarm(struct t_pal_alarm *alarm)
{
  return isexist_alarm(alarm,(uint8 *)"4000"); //输入电压4000
}
uint8 pal_getac_input_v_lowlarm(struct t_pal_alarm *alarm)
{
  return isexist_alarm(alarm,(uint8 *)"4000"); //输入电流4000
}
uint8 pal_getcurrent_overalarm(struct t_pal_alarm *alarm)
{
  return isexist_alarm(alarm,(uint8 *)"4002"); //输出电流过负荷
}
uint8 pal_getoutput_relay_state(struct ClientUserGunInfo * userinfo)
{
	return !userinfo->relay_state;//输出接触器状态 因为设备端1为闭合 0为断开  这边要做个取反处理
}
uint8 pal_gettemp_over_pro(struct t_pal_alarm *alarm)
{
  return isexist_alarm(alarm,(uint8 *)"3016"); //充电桩过温告警
}
uint8 pal_getshort_circuit_pro(struct t_pal_alarm *alarm)
{
  return 0;//短路保护
}
uint8 pal_getleakage_pro(struct t_pal_alarm *alarm)
{
  return 0;//漏电保护
}
uint8 pal_getstopbutton_trigger(struct t_pal_alarm *alarm)
{
  return isexist_alarm(alarm,(uint8 *)"3003");//急停按钮被按下
}
uint16 pal_getwork_state(struct ClientUserGunInfo * userinfo)
{
	return 0x00;//先不填
}
uint16 pal_getoutput_voltage(struct ClientUserGunInfo * userinfo)
{
	uint16 vlotage = userinfo->dc_charge_voltage/10;
  //printf("vlotage:%d\r\n",vlotage);
  return vlotage;
}
uint16 pal_getoutput_current(struct ClientUserGunInfo * userinfo)
{
  uint16 current = userinfo->dc_charge_current;
  //printf("current:%d\r\n",current);
	return current;
}
uint16 pal_getvoltage_A(struct ClientUserGunInfo * userinfo)
{
    return 0;
}
uint16 pal_getvoltage_B(struct ClientUserGunInfo * userinfo)
{
    return 0;
}
uint16 pal_getvoltage_C(struct ClientUserGunInfo * userinfo)
{
    return 0;
}
uint16 pal_getcurrent_A(struct ClientUserGunInfo * userinfo)
{
    return 0;
}
uint16 pal_getcurrent_B(struct ClientUserGunInfo * userinfo)
{
    return 0;
}
uint16 pal_getcurrenr_C(struct ClientUserGunInfo * userinfo)
{
    return 0;
}

uint32 pal_gettotal_actually_power(struct ClientUserGunInfo * userinfo)//保留小数点后两位位
{
	  uint32 totalpower = userinfo->meter_kwh_num;
	  //printf("totalpower:%d\r\n",totalpower);
    return totalpower;
}
uint32  pal_getelecvalue_outside(struct ClientUserGunInfo * userinfo)
{
   return 0;
}

uint8 pal_callback_timesync(uint8 *time)
{
	cp56time2a cpTime ;
	transtime(&cpTime,time);
	printf("pal_callback_timesync: %d-%d-%d %d:%d:%d   %d\r\n",cpTime.year,cpTime.month,cpTime.mday,cpTime.hour,cpTime.min,cpTime.msec,cpTime.wday);	
	
	int year = cpTime.year+2000;
	uint8 mon = cpTime.month;
	uint8 day = cpTime.mday;
	uint8 hour = cpTime.hour;
	uint8 min = cpTime.min;
	
	uint16 sec = cpTime.msec/1000;
  sec = (sec >= 60)?0:sec;
	
	uint8 week = cpTime.wday;
	return charger_sync_timedate(year,mon,day,hour,min,sec,week);
}
uint8 pal_callback_startcharge(uint8 gunno)
{
	 return charger_start_charge(gunno+1, 0);
}
uint8 pal_callback_stopcharge(uint8 gunno)
{
  return charger_stop_charge_by_server(gunno+1);
}
uint8  pal_callback_set_net_state(uint8_t state)
{
	 dev_set_net_state(state);
   return 0;
}

static void transtimetocp56(uint8 *time ,int year,uint8_t mon, uint8_t wday,uint8_t day, uint8_t hour, uint8_t min, uint32 msec)
{
	cp56time2a cptime = {0};
	memset((uint8 *)&cptime,0,sizeof(cptime));
		
  cptime.year = year;
  cptime.month = mon;
  cptime.wday = wday;
  cptime.mday = day;
  cptime.hour = hour;
  cptime.min = min;
  uint16  mSec = msec;
  cptime.msec = ((mSec & 0xff)<<8) | ((mSec >>8) &0xff);
	
	for(uint32 i = 0;i<7;i++)
	{
		  uint8 *ptime = (uint8 *)&cptime;
		  time[i] = *(ptime+(7-1-i));
	}
}
uint8  pal_callback_get_time_cp56(uint8 *time)
{
	T_TimeFmt tim;
	rtc_get_current_time(&tim);
	
	transtimetocp56(time,tim.year,tim.month,tim.week,tim.date,tim.hour,tim.min,tim.sec*1000);
	return 1;
}
uint32 pal_save_history_bill(uint8 *trade_num)//把当前记录存储到记录历史记录
{
	 uint8 tardenum[40] = {0};
	 BCD_to_ASC(tardenum,(const uint8*)trade_num,16);//交易流水号是16位的
		
	 record_save_to_history((char *)tardenum);
	 return 0;
}
uint32 pal_save_current_bill(uint8 *trade_num,uint8 *data,uint32 len)//更新记录
{
	  uint8 asc[1024] = {0};
	  BCD_to_ASC(asc,(const uint8*)data,len);
			
		uint8 tardenum[40] = {0};
	  BCD_to_ASC(tardenum,(const uint8*)trade_num,16);//交易流水号是16位的
		
    record_update((char *)tardenum, (char *)asc);
    return 0;
}
uint8 pal_read_bill(uint8 *buf)//读取充电记录
{
	 #define name_len 50 
   #define name_cnt 1
   
   char_t name_list[name_len*name_cnt]={0};
   int32_t num = record_read_charging_file_list(name_list, name_len, name_cnt);

   for(int32_t i=0;i<num;i++){
   printf("callback_read_bill name:%s\r\n", name_list + name_len*i);
   char asc[1024] = {0}; 
   if (record_read_charging_whth_dno(name_list + name_len*i, asc, sizeof(asc)) > 0){//读取记录
		  ASC_to_BCD(buf,(const uint8 *)asc,strlen(asc));
		  return 1;
	 }
    }
    return 0;
}
void transtime(cp56time2a *cptime ,const uint8 *time)
{
	  uint8 timetmp[7] = {0};
		for(uint32 i = 0;i<7;i++)
		{
			timetmp[i] = time[7-1-i];
		}
    uint8 tmp = timetmp[1];
    timetmp[1] = timetmp[0];
    timetmp[0]=tmp;

		memcpy((uint8 *)cptime,timetmp,sizeof(timetmp));
  
}
uint8 pal_istime(uint8 *time)
{
	  if(strlen((char *)time) == 0)
		{
			 return 0;
		}
	  cp56time2a cptime = {0};
		transtime(&cptime,time);
		
	  T_TimeFmt time_src;
		time_src.year = cptime.year;
		time_src.month = cptime.month;
		time_src.date = cptime.mday;
		time_src.week = cptime.wday;
		time_src.hour = cptime.hour;
		time_src.min = cptime.min;
		time_src.sec = cptime.msec/1000;
    time_src.sec  = (time_src.sec  >= 60)?0:time_src.sec ;
		
	  T_TimeFmt time_now;
	  rtc_get_current_time(&time_now);
	
  	printf("time_src: %d-%d-%d %d:%d:%d   %d\r\n",time_src.year,time_src.month,time_src.date,time_src.hour,time_src.min,time_src.sec,time_src.week);	
	  printf("time_now: %d-%d-%d %d:%d:%d   %d\r\n",time_now.year,time_now.month,time_now.date,time_now.hour,time_now.min,time_now.sec,time_now.week);	
		uint32 sec_src= RTC_GetTimeFmtSec(&time_src);
		uint32 sec_now= RTC_GetTimeFmtSec(&time_now);
		
	  printf("sec_sec = %d\r\n",sec_src);
	  printf("sec_now = %d\r\n",sec_now);
		
		if(sec_now >sec_src)
		{
			 return 1;
		}
		return 0;
}
uint32 gettotalsec(uint8 *time)
{
	  cp56time2a cptime = {0};
		transtime(&cptime,time);
		
	  T_TimeFmt time_src;
		time_src.year = cptime.year;
		time_src.month = cptime.month;
		time_src.date = cptime.mday;
		time_src.week = cptime.wday;
		time_src.hour = cptime.hour;
		time_src.min = cptime.min;
		time_src.sec = cptime.msec/1000;
    time_src.sec  = (time_src.sec  >= 60)?0:time_src.sec ;
		
		
		return RTC_GetTimeFmtSec(&time_src);
}
